import org.apache.spark.SparkConf
import org.apache.spark.sql.{SaveMode, SparkSession}


object gpa_num {
  def main(args: Array[String]): Unit = {
    val sparkconf = new SparkConf()
      .setMaster("local[*]")
    val sparksession = SparkSession
      .builder()
      .config(sparkconf)
      .enableHiveSupport()
      .appName("gpa_num")
      .getOrCreate()

    var df = sparksession.sql(
      """
        | SELECT
        |    CASE
        |        WHEN gpa < 3.5  THEN '低于平均绩点'
        |        WHEN gpa <=  3.6 THEN '处于平均绩点'
        |        WHEN gpa >  3.6 THEN '高于平均绩点'

        |    END AS gpa,
        |    COUNT(*) AS num
        |FROM
        |    db_stu.tb_stu
        |GROUP BY
        |    CASE
        |         WHEN gpa < 3.5  THEN '低于平均绩点'
        |        WHEN gpa <=  3.6 THEN '处于平均绩点'
        |        WHEN gpa >  3.6 THEN '高于平均绩点'
        |
        |    END
        |ORDER BY
        |    gpa;
        |""".stripMargin
    )
    val price_view = df.createTempView("price_view")
    val gpa_num = sparksession.sql("select  *  from price_view  ")
    //dws_cardio_age.show()
    //-将结果进行存储，存储到 hive 数据库中

    gpa_num.write.mode(SaveMode.Overwrite).saveAsTable("db_stu.gpa_num")



    sparksession.stop()
    sparksession.close()
  }
}
